//
//  YPApplication.swift
//  YPSwift
//
//  Created by ogawa on 2020/4/28.
//  Copyright © 2020 ogawa. All rights reserved.
//

import Foundation

final class YPApplication: NSObject {
    
     static let shared = YPApplication()
    
     var window: UIWindow?
    
     private override init() {
          super.init()
     }

    /**
     *模块化基础配置
     */
     func configModules() {
        YPEnvs.baseUrl = ["http://seongbrave.cn/api/v1/"]
        YPEnvs.isDebug = true
        YPNetWorkCore.baseUrl = YPEnvs.baseUrl
        YPGlobal.shared.updateUserFromService()
        //错误码为200010 表示回话过期
        YPCoreM.sharedInstance.toLoginErrorCode = 200010;
     }

    /**
     * 设置主题颜色
     */
    func configAppTheme(){
        var theme = ThemeType.currentTheme()
        if theme.isDark != true {
          theme = theme.toggled()
        }
        theme = theme.withColor(color: .green)
        themeService.switch(theme)
    }
    
    /**
     * 设置导航等 的主题颜色
     */
    func configAppleance(){
        
        let navigationBarAppearance = UINavigationBar.appearance()
        //设置显示的颜色
        navigationBarAppearance.barTintColor = UIColor.white
        navigationBarAppearance.tintColor = UIColor.black
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]

        let tabBarAppearance = UITabBar.appearance()
        tabBarAppearance.tintColor = UIColor.black
        tabBarAppearance.barTintColor = UIColor.white
        tabBarAppearance.isOpaque = true
        
    }
    
    func presentInitialScreen(in window: UIWindow?) {
        self.window = window;
        setHomeTabBarVc();
    }
    
    func setHomeTabBarVc(){
        self.window?.rootViewController = YPBuzTabBar_vc()
        self.window?.makeKeyAndVisible()
    }
    
}

extension UIApplication {
    //MRAK: 强制旋转屏幕
    class func changeOrientationTo(landscapeRight: Bool) {
        guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
        if landscapeRight == true {
            delegate.orientation = .landscapeRight
            UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        } else {
            delegate.orientation = .portrait
            UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        }
    }
}
